<?php
// +----------------------------------------------------------------------
// | Life Is A Box Of Chocolate...
// +----------------------------------------------------------------------
// | Company: YG | User: Sody xiezhen  |  Email:2918433292@qq.com  | Time:2017/3/21 19:19
// +----------------------------------------------------------------------
// | TITLE: this to do?
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Config;
use think\Controller;
use think\Request;
use think\Url;
use weixinmp\WechatAuth;

class Weixin extends Controller{
    protected $app_id;
    protected $app_secret;

    public function __construct()
    {
        parent::__construct();
        $this->app_id = Config::get('weixin.app_id');
        $this->app_secret = Config::get('weixin.app_secret');
    }

    public function index(){
        return 'index weixin index';
    }
    //哈基石 o3qMbxCXrLwYnRpUN62u7zb3NBxE
    public function getUserList(){
        $weixinAuth = new WechatAuth($this->app_id,$this->app_secret);
        $weixinAuth->getAccessToken();
        $data = $weixinAuth->userGet();
        dump($data);
    }
    public function getUserInfo(){
        $openID = 'o3qMbxCXrLwYnRpUN62u7zb3NBxE';
//        $access_token = 'S_QUYVubAjDHjvoMChtHCzkbdRxuNNT-Rwr6eQF0g9iW2k5e3yGaBInKtQMwX-rnhkORZ7yR4i0cIAiSw_XZ6sicbNcvrovfNtKL9BIcM3cXPXiABAJBJ';
        $weixinAuth = new WechatAuth($this->app_id,$this->app_secret);
        $access_token = $weixinAuth->getAccessToken();
//        dump($access_token);exit();
        $data = $weixinAuth->userinfo($openID);
        dump($data);
    }

    public function uploadImage(){
        $weixinAuth = new WechatAuth($this->app_id,$this->app_secret);
        $weixinAuth->getAccessToken();
        $imagePath = ROOT_PATH.'public/static/images/liuyifei.jpg';
        $result = $weixinAuth->materialAddMaterial($imagePath,'image','茜茜公主');
        dump($result);
    }
    public function entrance(){
        $weixinAuth = new WechatAuth($this->app_id,$this->app_secret);
        $url = Url::build('getUserInfoSubscribed','',true,true);
        return redirect($weixinAuth->getRequestCodeURL($url,null,'snsapi_base'));
    }
    public function getUserInfoSubscribed(){
        $request = Request::instance();

        $code = $request->get('code');
        $weixinAuth = new WechatAuth($this->app_id,$this->app_secret);
        $arr = $weixinAuth->getAccessToken('code',$code);
        $openID = $arr['openid'];
        $access_token = $arr['access_token'];
        $userInfo = $weixinAuth->getUserInfo($openID);
        $this->assign('user',$userInfo);
        return $this->fetch('showinfo');
    }
    public function showInfo(){
        $arr = [
            'openid'=>'o3qMbxCXrLwYnRpUN62u7zb3NBxE',
            'nickname'=>'哈基石',
            'country'=>'中国',
            'province'=>'湖北',
            'headimgurl'=>'http:\/\/wx.qlogo.cn\/mmopen\/tujibI9qO2TFlQSicvbTbDuBrj698mL3efWZViap7TDPDUSUdxacKicrNN42ibuZe714Rib8wv7Qrx7l9HIACzDhib1YCQhWyAKv8L7\/0',
        ];
        $this->assign('user',$arr);
        return $this->fetch();
    }
}